package com.colt.usercenter.auth;

import com.colt.usercenter.security.RegistorExcepion;
import com.colt.usercenter.security.SignException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.security.auth.login.LoginException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionErrorHandler {
    @ExceptionHandler(SignException.class)
    public ResponseEntity<ErrorBody> signError(SignException e){
        /**
        * @Description: 签到出现异常
        * @Param: [e]
        * @return: org.springframework.http.ResponseEntity<com.colt.usercenter.auth.ErrorBody>
        * @Author: colt
        * @Date: 2021/5/5
        */
        return new ResponseEntity<ErrorBody>(
                ErrorBody.builder()
                .body("今天已经签到过了，明天再来吧~")
                .status(cn.hutool.http.HttpStatus.HTTP_BAD_METHOD)
                .build()
                ,HttpStatus.UNAUTHORIZED
        );
    }
    @ExceptionHandler(SecurityException.class)
    public ResponseEntity<ErrorBody> error(SecurityException e){
        /**
        * @Description: 自定义身份认证异常
        * @Param: [e]
        * @return: org.springframework.http.ResponseEntity<com.colt.usercenter.auth.ErrorBody>
        * @Author: colt
        * @Date: 2020/11/26
        */
        log.warn("发生SecurityException异常",e);
        return new ResponseEntity<ErrorBody>(
                ErrorBody.builder()
                .body(e.getMessage())
                .status(HttpStatus.UNAUTHORIZED.value())
                .build(),
                HttpStatus.UNAUTHORIZED
        );
    }

    @ExceptionHandler(LoginException.class)
    public ResponseEntity<ErrorBody> errorLogin(LoginException e){
        /**
        * @Description: 自定义登录异常
        * @Param: [e]
        * @return: org.springframework.http.ResponseEntity<com.colt.usercenter.auth.ErrorBody>
        * @Author: colt
        * @Date: 2020/11/26
        */
        log.warn("LoginException",e);
        return new ResponseEntity<ErrorBody>(
                ErrorBody.builder()
                .body("登录参数非法，拒绝访问哦~")
                .status(HttpStatus.UNAUTHORIZED.value())
                .build(),
                HttpStatus.UNAUTHORIZED
        );
    }
    @ExceptionHandler(RegistorExcepion.class)
    public ResponseEntity<ErrorBody> registorError(RegistorExcepion e){
        /**
        * @Description: 自定义注册异常
        * @Param: [e]
        * @return: org.springframework.http.ResponseEntity<com.colt.usercenter.auth.ErrorBody>
        * @Author: colt
        * @Date: 2020/11/26
        */
        log.warn("发生注册异常~",e);
        return new ResponseEntity<ErrorBody>(
                ErrorBody.builder()
                        .body("注册参数非法，无法注册哦~")
                        .status(HttpStatus.ALREADY_REPORTED.value())
                        .build(),
                HttpStatus.ALREADY_REPORTED
        );
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
class ErrorBody{
    /*
    * 异常内容
    * */
    private String body;
    /*
    * 异常状态码
    * */
    private int status;
}
